using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace Webshop20.Infrastructure.Persistence.NH.UserTypes
{
    public class Serializer
    {
        public static byte[] Serialize(object objectToSerialize)
        {
            MemoryStream stream;
            using(stream = new MemoryStream())
            {
                BinaryFormatter formatter = new BinaryFormatter();
                formatter.Serialize(stream, objectToSerialize);
            }

            return stream.ToArray();

        }

        public static object Dezerialize(byte[] bytesToDezerialize)
        {
            object objectGraph;
            using (MemoryStream  stream = new MemoryStream(bytesToDezerialize))
            {
                BinaryFormatter formatter = new BinaryFormatter();
                objectGraph = formatter.Deserialize(stream); 
            }

            return objectGraph;
        }
    }
}